;*****************************************************************************************************
;+
; NAME:
;       IGL_COLORBAR
;
; PURPOSE:
;
;       This is a basic vizualisation object which represents a
;       color bar. It is subclassed from CATCOLORBAR (Catalyst
;       Library) since it was an already nice implementation of
;       a color bar object.
;
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
; SUPERCLASSES:
;
;       CATCOLORBAR (Catalyst Library)
;
; SYNTAX:
;
;       colorBar = Obj_New('IGL_ColorBar')
;
;
; CLASS_STRUCTURE:
;
;       class = {IGL_ColorBar, $      
;           INHERITS CATCOLORBAR $   ; This is a CATCOLORBAR object
;          }
;
;
; MODIFICATION_HISTORY:
;
;       Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::CONTROLPANEL
;
; PURPOSE:
;
;       This method creates a control panel for the graphic.
;
; SYNTAX:
;
;       self -> ControlPanel, baseObject
;
; ARGUMENTS:
;
;       baseObject:    The object reference of a base widget for this control to
;                      be added to. If not supplied, the control panel will be in a
;                      self contained window.
;
; KEYWORDS:
;
;       _EXTRA:       Any keywords appropriate for the CATCONTROLPANEL::INIT method.
;-
;*****************************************************************************************************
PRO IGL_ColorBar::ControlPanel, baseObject, _EXTRA=extraKeywords

   @cat_pro_error_handler

   ;; Create a new control panel
   cp = OBJ_NEW ('CatControlPanel', self, PARENT=baseObject, COLUMN=1, $
      TITLE='IGL_Colorbar Control Panel', _EXTRA=extraKeywords, /No_Cancel, /No_Apply, /No_OK)

   IF (NOT OBJ_VALID (cp)) THEN RETURN

   aproperties = Obj_New('PROPERTYSHEETWIDGET', cp, Value=self, Name='IGL_COLORBAR PROPERTYSHEET', YSize=19)
   aproperties -> SetProperty, Event_Object=self

   ;; Display the control panel if it created its own TLB.
   IF cp -> Created_Own_TLB(tlb) THEN tlb -> Draw, /Center

   self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::DRAW
;
; PURPOSE:
;
;       This method draws the graphic in the current display window.
;
; SYNTAX:
;
;       self -> Draw
;
; ARGUMENTS:
;
;       None.
;
; KEYWORDS:
;
;       _EXTRA:  Any keyword accepted by the CATATOM and CATCOLORBAR
;                draw methods.
;
;-
;*****************************************************************************************************
PRO IGL_ColorBar::Draw, _EXTRA=extra

  @cat_pro_error_handler

  ;; Save current color table
  TVLCT, r, g, b, /get

  ;; Switch to decomposed mode
  SetDecomposedState, 1, CurrentState=currentState

  ;; Process the title text with TexToIDL
  saveTitle = self.title
  self.title = texToIDL(self.title)

  ;; Scale the thickness if we are in PostScript
  thickness = self.thickness
  IF !D.Name EQ 'PS' THEN self.thickness *= 2.5

  ;; Switch to Hardware font if this is PostScript
  font = self.font
  IF (!D.Name EQ 'PS') AND (font EQ 0) THEN self.font = 1

  ;; Draw the color bar
  self -> CATCOLORBAR::Draw, _EXTRA=extra

  ;; Restore decomposed mode
  SetDecomposedState, currentState

  ;; Restore color table
  TVLCT, r, g, b

  ;; Load back original title
  self.title = saveTitle

  ;; Restore the thickness
  self.thickness = thickness

  ;; Restore the font
  self.font = font

  ;; Draw the children
  self -> CATATOM::Draw, _EXTRA=extra

  ;; Report completion
  self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;        IGL_ColorBar::EVENT_HANDLER
;
; PURPOSE:
;
;        This method is the event handler for the IGL_ColorBar object. It will typically
;        be used to respond to events from widget objects created in the CONTROLPANEL
;        method.
;
; SYNTAX:
;
;        This method is called automatically by the event handling mechanism.
;
; ARGUMENTS:
;
;       event: The event structure as described in the IDL help files, except
;              that the ID, TOP and HANDLER tags will be object references.
;
; KEYWORDS:
;
;       None.
;
;-
;*****************************************************************************************************
PRO IGL_ColorBar::EventHandler, event

   @cat_pro_error_handler

   ; Get the name of the widget generating the event. Branch on this.
   event.ID -> GetProperty, Name=eventName
   
   CASE eventName OF

      'IGL_COLORBAR PROPERTYSHEET': BEGIN

         IF event.type EQ 0 THEN BEGIN
            
            CASE StrUpCase(event.identifier) OF

               'COLOR': BEGIN

                  event.component -> GetProperty, Color=color
                  event.id -> GetProperty, ID=group_leader
                  color = PickColorName(color, Group_Leader=group_leader)
                  event.component -> SetProperty, Color=color

                  ;; Refresh the graphics hierarchy
                  IGL_RefreshDraw, event.component, /Buffered_Output

               END

               'POSITION': BEGIN
                  event.component -> GetProperty, Position=pos
                  event.id -> GetProperty, ID=group_leader
                  position = AdjustPosition(pos, Group_Leader=group_leader)
                  event.component -> SetProperty, Position=position

                  ;; Refresh the graphics hierarchy
                  IGL_RefreshDraw, event.component, /Buffered_Output

               END

               'CTINDEX': BEGIN
                  event.component -> GetProperty, CTINDEX=ctindex
                  self._colors -> XColors, Group_Leader=event.id
                  self._colors -> GetProperty, CTINDEX=ctindex
                  event.component -> SetProperty, CTINDEX=ctindex

                  ;; Refresh the graphics hierarchy
                  IGL_RefreshDraw, event.component, /Buffered_Output

               END
               
               ELSE: BEGIN

                  component = event.component
                  identifier = event.identifier
                  event.id -> GetProperty, Value=value, Component=component, Property_Value=identifier
                  event.component -> SetPropertyByIdentifier, identifier, value

                  ;; Refresh the graphics hierarchy
                  IF OBJ_VALID(event.component) THEN IGL_RefreshDraw, event.component, /Buffered_Output

               END

            ENDCASE

         ENDIF                  ; of BUTTON DOWN EVENT

      END                 

      ;; If you can't handle the event here. Pass it along to superclass EventHandler
      ELSE: self -> CATCOLORBAR::EventHandler, event

   ENDCASE

   IF Obj_Valid(self) THEN self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::GETPROPERTY
;
; PURPOSE:
;
;       This method is used to get the object's properties.
;
; SYNTAX:
;
;       self -> GetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     _REF_EXTRA:  Any keywords appropriate for the superclass GetProperty method.
;-
;*****************************************************************************************************
PRO IGL_ColorBar::GetProperty, _REF_EXTRA=extraKeywords

   @cat_pro_error_handler

   IF (N_ELEMENTS(extraKeywords) GT 0) THEN self -> CATCOLORBAR::GetProperty, _EXTRA=extraKeywords

   self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::SETPROPERTY
;
; PURPOSE:
;
;       This method is used to set the object's properties.
;
; SYNTAX:
;
;       self -> SetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     DELETE:      Set this keyword to destroy the object.
;
;     _EXTRA:      Any keyword accepted by the SetProperty method of
;                  the CATCOLORBAR superclass.
;-
;*****************************************************************************************************
PRO IGL_ColorBar::SetProperty, DELETE=delete, _EXTRA=extraKeywords

   @cat_pro_error_handler

   IF (N_ELEMENTS(extraKeywords) GT 0) THEN self -> CATCOLORBAR::SetProperty, _EXTRA=extraKeywords

   IF (keyword_set(delete)) THEN BEGIN
      self -> SetProperty, Visible=0
      IGL_RefreshDraw, self, /Buffered_Output
      Obj_Destroy, self._controlPanel
      self -> GetProperty, Parent=parents
      self -> SendMessage, 'OBJECT_DELETED', DATA=self
      FOR j=0,N_Elements(parents)-1 DO IF OBJ_VALID(parents) THEN parents[j] -> Remove, self
      Obj_Destroy, self
      RETURN
   ENDIF

   IF (obj_valid(self)) THEN self -> Report, /Completed

END


;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::CLEANUP
;
; PURPOSE:
;
;       This is the IGL_ColorBar object class destructor method.
;
; SYNTAX:
;
;       Called automatically when the object is destroyed.
;
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     None.
;-
;*****************************************************************************************************
PRO IGL_ColorBar::CLEANUP

   @cat_pro_error_handler

   ;; Cleanup the superclass
   self -> CATCOLORBAR::CLEANUP

   ;; Report completion
   self -> Report, /Completed
   
END


;*****************************************************************************************************
;+
; NAME:
;       IGL_ColorBar::INIT
;
; PURPOSE:
;
;       This is the IGL_ColorBar object class initialization method
;
; SYNTAX:
;
;       Called automatically when the object is created.
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     _EXTRA:      Any keywords appropriate for the CATCOLORBAR::Init method.
;-
;*****************************************************************************************************
FUNCTION IGL_ColorBar::INIT, COLOR=color, $
                             _Extra=extraKeywords


   @cat_func_error_handler

   ;; Default values
   IF (n_elements(color) EQ 0) THEN color = !IGL_P.color

   ;; Save the current color table
   TVLCT, r ,g, b, /GET

   ;; Call superclass INIT method
   ok = self -> CATCOLORBAR::Init(COLOR=color,_Extra=extraKeywords)

   IF (NOT ok) THEN RETURN, 0

   ;; We don't need the color object because the IGL_Window
   ;; will handle the colors.
   self -> GetProperty, COLOR_OBJECT=colorTool
   IF OBJ_VALID(colorTool) THEN OBJ_DESTROY, colorTool

   ;; Don't need the modemap because the only possible
   ;; interaction is the move interaction.
   OBJ_DESTROY, self.modemap

   ;; Change default name and description for the CATCOLORBAR object
   self -> SetProperty, name='ColorBar', description='Color bar graphic'

   ;; Set up editable properties
   self -> SetPropertyAttribute, 'NAME', sensitive=0
   self -> SetPropertyAttribute, 'DESCRIPTION', sensitive=0
   self -> SetPropertyAttribute, 'CTINDEX', /HIDE

   ;; Restore the color table
   TVLCT, r, g, b

   ;; Report completion
   self -> Report, /Completed

   RETURN, 1

END


;*****************************************************************************************************
;
; NAME:
;       IGL_COLORBAR CLASS DEFINITION
;
; PURPOSE:
;
;       This is the IGL_Colorbar object's structure definition code.
;
;*****************************************************************************************************
PRO IGL_ColorBar__DEFINE, class

  COMPILE_OPT idl2, HIDDEN

  class = {IGL_ColorBar, $      
           INHERITS CATCOLORBAR $   ; This is a CATCOLORBAR object
          }

  struct_hide, class

END
